package antrace;

import processing.core.PImage;
import javax.microedition.lcdui.game.Sprite;

public class AntSprite extends Sprite implements InterfaceGameSprite {

	public static final int STATE_IDLE = 0;
	public static final int STATE_WALKING = 1;
	public static final int STATE_DIGGING = 2;
	public static final int STATE_DEAD = 3;
	
	public static final int NORMAL_SPEED = 3;
	public static final int DIGGING_SPEED = 1;
	
	//BONUSAR
	/** Skobonus, gör att man går snabbare */
	public static final int BONUS_BOOT = 100;

	public static final int DIRECTION_RIGHT = 0;	// DIRECTION
	public static final int DIRECTION_DOWN = 1;		//     3
	public static final int DIRECTION_LEFT = 2;		//   2 + 0
	public static final int DIRECTION_UP = 3;		//     1

	private final int[] sequenceWalking = { 0, 0, 1, 1, 2, 2 };
	private final int[] sequenceDigging = { 3, 3, 4, 4 };
	private final int[] sequenceIdle = { 0 };
	private final int[] sequenceDead = { 5 };

	private int state;
	private int world_x;
	private int world_y;
	private int direction;
	private int lockedDirection;
	private int speed;
	private int score;
	private int bonusBoot;
	private boolean activatedBoot=false;
	
	/** TODO lägg till kommentar här om vad delta_x är! */
	private int delta_x;
	public final int fixedCenterPositionX = AntRace.midlet.width/2 - this.getWidth()/2;

	private boolean stateChanged;
	private boolean dead;

	public AntSprite(PImage img) {
		super(img.image, img.height, img.height);

		world_x = 0;
		world_y = 200;

		dead = false;
		direction = 0;
		delta_x = 0;

		defineReferencePixel(img.height-1, img.height/2);

		setState(STATE_IDLE);
		stateChanged = true;
	}

	public AntSprite(PImage img, int x0, int y0) {
		this(img);
		setPosition(x0, y0);
		world_x = x0;
		world_y = y0;
	}
	
	public void resetGame() {
		world_x = 0;
		world_y = 200;
		dead = false;
		direction = 0;
		lockedDirection = 10;
		delta_x = 0;
		score = 0;
		setState(STATE_IDLE);
		stateChanged = true;
	}
	
	public void achieveBonus(int b) {
		//mer bonusar kommer senare antagligen
		switch (b) {
		case BONUS_BOOT:
			bonusBoot = 1;
		}
	}
	
	public void useBonus(int b) {
		switch (b) {
		case BONUS_BOOT:
			activatedBoot = true;
		}
	}
	
	public int currentBonus() {
		// TODO: kolla vilken bonusXXXXXX som har värdet 1 returnera dess final int ex. BONUS_BOOT
		if (bonusBoot > 0) {
			return BONUS_BOOT;
		}
		
		return 0;
	}

	public int getRefPixelX() {
		return world_x - this.getX() + super.getRefPixelX();
	}

	public int getWorldX() {
		return world_x;
	}

	public int getWorldY() {
		return world_y;
	}
	
	public void setWorldX(int new_x){
		world_x = new_x;
	}

	public int getDelta_X() {
		return delta_x;
	}
	
	public int getLockedDirection() {
		return lockedDirection;
	}
	
	public void setLockedDirection(int d) {
		lockedDirection = d;
	}


	public void kill() {
		setState(STATE_DEAD);
		dead = true;
		delta_x = 0;
	}

	public boolean isDead() {
		return dead;
	}

	public void revive() {
		setState(STATE_IDLE);
		dead = false;
	}
	
	public void addScore(int s) {
		score += s; 
	}
	
	public int getScore() {
		return score;
	}

	public void move(int dx, int dy, boolean useSpeed) {
		if (dx == 0 && dy == 0 && !dead) {
			setState(STATE_IDLE);
		}

		if (useSpeed) {
			delta_x = speed*dx;
			world_x += delta_x;
			world_y += speed*dy;
		}
		else {
			delta_x = dx;
			world_x += delta_x;
			world_y += dy;
		}	

		// Fix för att inte kunna gå utanför bild i höjdled.
		// Skall vara 300 här på world_y. Det ser konstigt ut på emulatorn,
		// men blir rätt på mobilen.
		if (world_y > 300) {
			world_y = 300;
		}
		else if (world_y < 80) {
			world_y = 80;
		}
		if (world_x < 0) {
			world_x = 0;
		}

		if (dx > 0) {
			direction = DIRECTION_RIGHT;
		}
		else if (dy > 0) {
			direction = DIRECTION_DOWN;
		}
		else if (dx < 0) {
			direction = DIRECTION_LEFT;
		}
		else if (dy < 0) {
			direction = DIRECTION_UP;
		}
	}

	public void setState(int st) {
		if (st != state) {
			state = st;
			stateChanged = true;
		}
	}

	public int getVelocity() {
		return speed;
	}
	
	public void setVelocity(int v) {
		speed = v;
	}

	public int getDirection() {
		return direction;
	}
	
	// FIXME WORK IN PROGRESS
	public void update() {
		if (stateChanged) {
			switch (state) {
			case STATE_IDLE:
				setFrameSequence(sequenceIdle);
				speed = NORMAL_SPEED;
				
				if (activatedBoot) {
					speed += 2*speed;
				}
				
				break;
			case STATE_WALKING:
				setFrameSequence(sequenceWalking);
				speed = NORMAL_SPEED;
				
				if (activatedBoot) {
					speed += 2*speed;
				}
				
				break;
			case STATE_DIGGING:
				setFrameSequence(sequenceDigging);
				speed = DIGGING_SPEED;
				//temp fix till speedBoot (när man gräver ingen mer speeeeduuu!!
				if (activatedBoot) {
					activatedBoot = false;
					bonusBoot = 0;
				}
				break;
			case STATE_DEAD:
				setFrameSequence(sequenceDead);
				speed = 0;
				break;
			}
			
			
			stateChanged = false;
		}

		switch (direction) {
		case DIRECTION_RIGHT:
			setTransform(TRANS_NONE);
			break;
		case DIRECTION_DOWN:
			setTransform(TRANS_MIRROR_ROT270);
			break;
		case DIRECTION_LEFT:
			setTransform(TRANS_MIRROR);
			break;
		case DIRECTION_UP:
			setTransform(TRANS_MIRROR_ROT90);
			break;
		}

		if (world_x < fixedCenterPositionX) {
			setPosition(world_x, world_y);
		}
		else { 
			if (world_x >= fixedCenterPositionX && (world_x - delta_x) < fixedCenterPositionX) {
				delta_x = world_x - fixedCenterPositionX;
				world_x = fixedCenterPositionX;
			}
			setPosition(fixedCenterPositionX, world_y);
		}

		nextFrame();
	}

}
